<template>
    <!-- 修改资料 -->

    <div>
        <el-form label-width="85px" :model="form" label-position="right" ref="formRef" :rules="formRules">
            <tsCard header="基本信息">
                <el-row>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="编号" prop="number">
                            <el-input v-model="form.number"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="标题" prop="name">
                            <el-input v-model="form.name"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="副标题" prop="subtitle">
                            <el-input v-model="form.subtitle"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="发布时间" prop="effectDateTime">
                            <el-date-picker v-model="form.effectDateTime" type="datetime" placeholder="请选择发布时间"
                                format="YYYY-MM-DD HH:mm:ss" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="内容类别" prop="category">
                            <el-cascader v-model="form.categoryIds" :options="configData.categoryTreeList" :props="props1" :show-all-levels="false" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="星级类别" prop="starRating">
                            <el-rate v-model="form.starRating" allow-half />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="顺序号" prop="itemIndex">
                            <el-input-number v-model="form.itemIndex" :min="0" />
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="自定义1" prop="customer1">
                            <el-input v-model="form.customer1"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="自定义2" prop="customer2">
                            <el-input v-model="form.customer2"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="自定义3" prop="customer3">
                            <el-input v-model="form.customer3"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="自定义4" prop="customer4">
                            <el-input v-model="form.customer4"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="接收类型" prop="receiverType">
                            <el-select v-model="form.receiverType" clearable placeholder="请选择：">
                                <el-option v-for="item in configData.receiverTypeList" :key="item.value" :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row> 
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="状态" prop="status">
                            <el-switch v-model="form.status" size="large" inline-prompt active-value="normal"
                                inactive-value="disable" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
                        <el-form-item label="备注" prop="remark">
                            <tsEditor v-model="form.remark"></tsEditor>
                        </el-form-item>
                    </el-col>
                </el-row>
            </tsCard>
            <tsCard header="上传附件">
                <tsMultiple  v-model="dataList"  :addTemplate="addTemplate" edit @updateIndex="updateIndex" @delete="deleteFile">
                    <template #tableContent>
                        <el-table-column prop="fileName" label="文件名"></el-table-column>
                        <el-table-column  label="图片" width="100">
                            <template #default="scope">
                                <el-image v-if="checkType(scope.row) == 'image'" alt="上传图片" :src="scope.row.fileFullPath"
                                    :preview-src-list="srcList" preview-teleported @click="openDialog(scope.row.fileFullPath)"></el-image>
                                <video v-else-if="checkType(scope.row) == 'video'" :src="scope.row.fileFullPath" controls
                                    style="width: 100%;height: 100%;"></video>
                            </template>
                        </el-table-column>
                        <el-table-column prop="fileKind" label="类别">
                            <template #default="scope">
                                <span v-if="scope.row.operationType == ''">{{ scope.row.fileKindName }}</span>
                                <el-select v-else v-model="scope.row.fileKind">
                                    <el-option v-for="item in configData.fileKindList" :key="item.value" :label="item.label"
                                        :value="item.value">
                                    </el-option>
                                </el-select>
                            </template>
                        </el-table-column>
                        <el-table-column prop="itemIndex" label="排序">
                            <template #default="scope">
                                <span v-if="scope.row.operationType == ''">{{ scope.row.itemIndex }}</span>
                                <el-input-number v-else v-model="scope.row.itemIndex" :min="1"></el-input-number>
                            </template>
                        </el-table-column>
                        <el-table-column prop="remark" label="备注">
                            <template #default="scope">
                                <span v-if="scope.row.operationType == ''">{{ scope.row.remark }}</span>
                                <el-input v-else v-model="scope.row.remark" placeholder="请输入备注"></el-input>
                            </template>
                        </el-table-column>
                        <el-table-column prop="remark" label="状态">
                            <template #default="scope">
                                <el-switch v-model="scope.row.status" size="large" inline-prompt active-value="normal"
                                    inactive-value="disable" :disabled="scope.row.operationType == ''"/>
                            </template>
                        </el-table-column>
                    </template>
                </tsMultiple>
            </tsCard>
        </el-form>

        <!-- </template> -->
        <!-- <template #footer> -->
        <div class="button-position">
            <el-button @click="updateVisible">取 消</el-button>
            <el-button type="primary" @click="updateNotice(formRef)">确 定</el-button>
        </div>
        <!-- </template> -->

        <!-- </myDialog> -->
    </div>
</template>


<script setup>
import { reactive, ref, onMounted } from 'vue';
import { ElNotification } from 'element-plus';
import Api from '@/api/model/system'
import tsCard from '@/components/tsCard/index.vue'
import tsEditor from '@/components/scEditor/index.vue'
import tsMultiple from '@/components/tsUpload/multipe.vue'
import { checkType } from '@/utils/checkType.js'
const emit = defineEmits(['updatetable'])
const props = defineProps({
    paramsData: {
        type: Object,
        defalut: {}
    }
})

// 添加用户数据的对象
const form = ref({
});
const configData = ref({})
const formRef = ref()
const formRules = reactive({
    number: [
        { required: true, message: "请输入编号", trigger: "blur" },
    ],
    name: [
        { required: true, message: "请输入标题", trigger: "blur" },
    ],
    remark: [
        { required: true, message: "请输入备注", trigger: "blur" },
    ],
})
const dataList = ref([])
const deleteList = ref([])
const addTemplate = ref({
})
const srcList = ref([])
const props1 = {
  children:'subTableRecords'
}
onMounted(() => {
    getInit()
})

const updateVisible = () => {
    emit('updatetable');
}

const getInit = async () => {
    let res = await Api.notice.detail.post(props.paramsData);
    console.log(res)
    if (res.code == '200') {
        configData.value = res.configData
        form.value = res.data
        dataList.value = res.dataList
        addTemplate.value = res.dtl
    }

}
const openDialog = (val) => {
    srcList.value.unshift(val)
}
const deleteFile = (val) =>{
    deleteList.value.push(val)
}
const updateIndex = () => {
    dataList.value.forEach((item, index) => {
        item.itemIndex = index + 1
    })
}
const restAddFormData = () => {
    formRef.value.resetFields();
}

// 上传
const updateNotice = async (formRef) => {
    await formRef.validate((valid, fields) => {
        if (valid) {
            updateNoticeData();
        }
    })
}

const updateNoticeData = async () => {
    console.log(form.value);
    var params = {
        bean: form.value,
        dataList: dataList.value.concat(deleteList.value)
    }
    var res = await Api.notice.update.post(params);
    if (res.code == 200) {
        restAddFormData();
        ElNotification.success({ title: '', message: '添加成功！', duration: 2000 });
        emit('updatetable', true);
    }
}
</script>

<style scoped></style>

